package frontend.pages.reports.charismasynch;

import backend.model.worklog.WorkLogMismatch;
import frontend.model.reports.ReportInputUIModel;
import frontend.pages.reports.base.AbstractReportForm;
import frontend.pages.reports.base.AbstractReportPanel;
import frontend.reports.XLSFileRowCreator;
import shared.util.CalendarUtils;
import org.apache.wicket.Component;
import org.apache.wicket.Localizer;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class CharismaSynchronizationPanel extends AbstractReportPanel<WorkLogMismatch> {

    private static String RESOURCE_KEY_REPORT_FILE_NAME = "report.fileName.charismaValidation";
    private DefaultWorkLogMismatchTableTemplate tableTemplate;
    private ReportInputUIModel uiModel;

    public CharismaSynchronizationPanel(String id) {
        super(id);
    }

    protected boolean shouldAddTotalHours() {
        return false;
    }

    protected Component createTotalHoursPanel() {
        return null;
    }

    @Override
    protected DefaultDataTable<WorkLogMismatch> getReportResultsTable() {
        if (tableTemplate == null) {
            tableTemplate = new DefaultWorkLogMismatchTableTemplate();
        }
        return new WorkLogMismatchTable("reportResultsTable", reportResults, tableTemplate);
    }

    @Override
    protected AbstractReportForm getReportForm() {
        uiModel = new ReportInputUIModel();
        return new CharismaSynchronizationForm("reportForm", uiModel, reportResults, reportResultsContainer);
    }

    @Override
    protected List<String> getReportFileHeader() {
        return tableTemplate.getHeader();
    }

    @Override
    protected Map<String, String> getReportDetails() {
        Map<String, String> reportDetails = super.getReportDetails();
        if (uiModel != null) {
            reportDetails.put(Localizer.get().getString(AbstractReportForm.RESOURCE_KEY_FIELD_LABEL_START_DATE, null),
                    CalendarUtils.formatDate(uiModel.getStartDate()));
            reportDetails.put(Localizer.get().getString(AbstractReportForm.RESOURCE_KEY_FIELD_LABEL_END_DATE, null),
                    CalendarUtils.formatDate(uiModel.getEndDate()));
            reportDetails.put(Localizer.get().getString(CharismaSynchronizationForm.PAGES_CHARISMA_SYNCHRONIZATION_REPORT_FORM_EMPLOYEE, null),
                    uiModel.getEmployee() == null ? "-" : uiModel.getEmployee().getDisplayLabel());
            reportDetails.put(Localizer.get().getString(CharismaSynchronizationForm.PAGES_CHARISMA_SYNCHRONIZATION_REPORT_FORM_TEAM, null),
                    uiModel.getTeam() == null ? "-" : uiModel.getTeam().getDisplayLabel());
        }
        return reportDetails;
    }

    @Override
    protected String getReportName() {
        return Localizer.get().getString("menu.reports.items.charismaSyncPage", null);
    }

    @Override
    protected List<Object[]> getReportFileData() {
        List<Object[]> reportFileData = new LinkedList<Object[]>();
        for (WorkLogMismatch workLogMismatch : reportResults) {
            XLSFileRowCreator rowCreator = new XLSFileRowCreator(7);
            Object[] row = rowCreator.newRow();
            rowCreator.addDateCell(row, 0, workLogMismatch.getDate());
            rowCreator.addDBModelCell(row, 1, workLogMismatch.getEmployee());
            rowCreator.addStringCell(row, 2, workLogMismatch.getEmployee().getIdentificationCard());
            rowCreator.addDBModelCell(row, 3, workLogMismatch.getEmployee().getTeam());
            rowCreator.addDBModelCell(row, 4, workLogMismatch.getEmployee().getTeam().getForeman());
            rowCreator.addHoursCell(row, 5, workLogMismatch.getLocalHours());
            rowCreator.addHoursCell(row, 6, workLogMismatch.getCharismaHours());
            reportFileData.add(row);
        }
        return reportFileData;
    }

    @Override
    protected String getReportFileName() {
        return Localizer.get().getString(RESOURCE_KEY_REPORT_FILE_NAME, null);
    }
}
